<template>
  <je-modal
    v-model:visible="visibleModule"
    width="460px"
    height="auto"
    title="添加事件"
    :resize="false"
    :maximizable="false"
    cancel-button
    :ok-button="addModule"
    @close="cancelModal"
  >
    <je-form ref="formRefModule" layout="vertical" autocomplete="off">
      <je-form-item label="名称" v-bind="validateInfos.name">
        <je-input v-model:value="modelRef.name" />
      </je-form-item>
      <je-form-item label="编码" v-bind="validateInfos.code">
        <je-input v-model:value="modelRef.code" />
      </je-form-item>
    </je-form>
  </je-modal>
</template>
<script>
  import { defineComponent, ref, reactive } from 'vue';
  import { Input, Modal, Form } from '@jecloud/ui';
  import { verifyData } from '@/helper/regular';

  const useForm = Form.useForm;
  export default defineComponent({
    name: 'ModalAdd',
    components: {
      JeModal: Modal,
      JeForm: Form,
      JeFormItem: Form.Item,
      JeInput: Input,
    },
    props: {},
    emits: ['changeData'],
    setup(props, { emit }) {
      //保存模块数据，关闭弹窗
      const visibleModule = ref(true);
      const modelRef = reactive({
        name: '',
        code: '',
      });
      const cancelModal = () => {
        visibleModule.value = false;
        emit('changeData', { type: 'cancel' });
      };
      const rulesRef = reactive({
        name: verifyData.name,
        code: [verifyData.required],
      });
      const { resetFields, validate, validateInfos } = useForm(modelRef, rulesRef, {
        onValidate: (...args) => {},
      });
      const addModule = ({ $modal, button }) => {
        const modal = $modal;
        //保存数据逻辑
        validate()
          .then(() => {
            emit('changeData', { type: 'save', name: modelRef.name, code: modelRef.code });
            modal.colse();
          })
          .catch((error) => {});
        return false;
      };

      return {
        validateInfos,
        resetFields,
        modelRef,
        cancelModal,
        addModule,
        visibleModule,
        centered: true,
      };
    },
  });
</script>
